package com.changgou.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Date;

/**
 * @Author zwh
 * @Date 2019/9/24 9:19
 * @Version 1.0
 **/
@Component
public class IpFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println(new Date().toLocaleString()+"第一个IP过滤器");
        ServerHttpRequest request = exchange.getRequest();
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        //获取请求的IP地址
        String hostName = remoteAddress.getHostName();
        InetAddress address = remoteAddress.getAddress();
        System.out.println("address = " + address);
        System.out.println("hostName = " + hostName);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
